package com.example.futureai.controller.chat;

import com.example.futureai.service.AiDocumentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

/**
 * @ClassName: UploadFileController
 * @Description:
 * @Author: YourName
 * @Date: 2025/2/24
 */
@RestController
@Slf4j
@RequestMapping("/aiUpload")
public class UploadFileController {

    @Autowired
    private AiDocumentService aiDocument;

    // 文件上传的保存路径（可以根据需要修改）
    private static String UPLOADED_FOLDER = "uploads/";

    // 文件上传接口
    @PostMapping("/file")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return new ResponseEntity<>("文件为空，请上传文件！", HttpStatus.BAD_REQUEST);
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();
        // 为了避免文件名冲突，可以使用UUID生成唯一的文件名
        String uniqueFileName = UUID.randomUUID().toString() + "_" + fileName;
        // 创建上传目录（如果不存在）
        File uploadDir = new File(UPLOADED_FOLDER);
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }
        // 构建文件保存路径
        Path filePath = Paths.get(UPLOADED_FOLDER + uniqueFileName);
        try {
            // 将文件保存到指定路径
            Files.write(filePath, file.getBytes());
            return new ResponseEntity<>("文件上传成功，保存路径为：" + filePath.toString(), HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity<>("文件上传失败：" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    @PostMapping("/fileInVector")
    public ResponseEntity<String> uploadFileInVector(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return new ResponseEntity<>("文件为空，请上传文件！", HttpStatus.BAD_REQUEST);
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            aiDocument.addDocument(inputStream, fileName);
            return new ResponseEntity<>("文件转向量数据成功", HttpStatus.OK);
        } catch (Exception e) {
            log.error("文件转向量数据失败", e);
            return new ResponseEntity<>("转Document失败：" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        } finally {
            assert inputStream != null;
            inputStream.close();
        }
    }
}
